package game.Audio;

import game.Player.Input;

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AudioResources {
    protected static List<Clip> dieSounds;
    protected static List<Clip> hitSounds;
    protected static List<Clip> stepSounds;
    protected static List<Clip> roundSounds;
    protected static Clip weaponChangeSound;
    protected static Clip tickSound;
    protected static Clip c4Sound, c4DisarmSound, c4ExplodeSound;

    public static void init() {
        try {
            loadDieClips();
            loadHitClips();
            loadStepClips();
            loadC4Clips();
            loadRoundClips();
            loadWeaponChangClip();

            tickSound = loadClip("audio/weapons/c4.wav");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Clip loadClip(String fileName)  {
        try {
            InputStream inputStream = AudioResources.class.getClassLoader().getResourceAsStream(fileName);
            InputStream bufferedIn = new BufferedInputStream(inputStream);

            // Audio Stream
            AudioInputStream a = AudioSystem.getAudioInputStream(bufferedIn);
            AudioFormat format = a.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);

            // Clip
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(a);   // Open music file

            return clip;
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void loadDieClips() {
        dieSounds = new ArrayList<Clip>();

        for(int i = 1; i <= 3; i++){
            String fileName = "audio/player/die" + i + ".wav";
            dieSounds.add(loadClip(fileName));
        }
    }

    public static void loadHitClips() {
        hitSounds = new ArrayList<Clip>();

        for(int i = 1; i <= 3; i++){
            String fileName = "audio/player/hit" + i + ".wav";
            hitSounds.add(loadClip(fileName));
        }
    }

    public static void loadStepClips() {
        stepSounds = new ArrayList<Clip>();

        for(int i = 1; i <= 4; i++){
            String fileName = "audio/player/pl_step" + i + ".wav";
            stepSounds.add(loadClip(fileName));
        }
    }

    public static void loadRoundClips() {
        roundSounds = new ArrayList<Clip>();
        String fileName = "audio/radio/go.ogg";
        roundSounds.add(loadClip(fileName));
        fileName = "audio/radio/letsgo.ogg";
        roundSounds.add(loadClip(fileName));
        fileName = "audio/radio/moveout.ogg";
        roundSounds.add(loadClip(fileName));
    }

    public static void loadWeaponChangClip() {
        String fileName = "audio/weapons/w_roundin.wav";
        weaponChangeSound = loadClip(fileName);
    }

    public static void loadC4Clips() {
        String fileName = "audio/weapons/c4.wav";
        c4Sound = (loadClip(fileName));
        fileName = "audio/weapons/c4_disarm.wav";
        c4DisarmSound = (loadClip(fileName));
        fileName = "audio/weapons/c4_explode.wav";
        c4ExplodeSound = (loadClip(fileName));
    }

    protected static Clip getRandomSound (List<Clip> list){
        int max = list.size();
        int rnd = (int)(Math.random() * max); // max not included

        return list.get(rnd);
    }
    protected static int getRandomIndex(List list){
        int max = list.size();
        int rnd = (int)(Math.random() * max); // max not included
        return rnd;
    }


    public static Clip getTickSound(){
        return tickSound;
    }
    public static Clip getHitSound(){
        return getRandomSound(hitSounds);
    }
    public static Clip getDieSound(){
        return getRandomSound(dieSounds);
    }
    public static Clip getStepSound(){
        return getRandomSound(stepSounds);
    }
    public static Clip getInitRoundSound(){
        return getRandomSound(roundSounds);
    }
    public static Clip getWeaponChangeSound() { return weaponChangeSound; }

    public static Clip getC4Sound() {
        return c4Sound;
    }

    public static Clip getC4DisarmSound() {
        return c4DisarmSound;
    }

    public static Clip getC4ExplodeSound() {
        return c4ExplodeSound;
    }

}
